package com.water.camera.view.temp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.plata.base.aclr.utils.SPHelper;
import com.plata.base.utils.WeakHandler;
import com.water.camera.R;
import com.water.camera.activity.PinEditAct;
import com.water.camera.adapter.TemplateAdapter;
import com.water.camera.bean.EditItem;
import com.water.camera.mark.BaseMark;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public abstract class BaseTemp extends BaseMark {

    WeakHandler h;

    public BaseTemp(@NonNull Context context) {
        super(context);
    }

    public BaseTemp(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseTemp(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public BaseTemp(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    @CallSuper
    protected void init(int type) {
        h = new WeakHandler();
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toEdit();
            }
        });
    }

    public void toEdit() {
        String name = BaseTemp.this.getClass().getSimpleName();
        Intent i = new Intent(getContext(), PinEditAct.class);
        i.putExtra("name", name);
        i.putExtra("color", getColor());
        i.putParcelableArrayListExtra("item", getEditItems());
        ((Activity) getContext()).startActivityForResult(i, 10);
    }

    protected abstract int getDefaultColor();

    protected int getColor() {
        int color = SPHelper.getInstance(getContext()).getInt(getClass().getSimpleName() + "_color", getDefaultColor());
        return color;
    }

    @Override
    @CallSuper
    public ArrayList<EditItem> getEditItems() {
        ArrayList list = super.getEditItems();
        JSONArray obj = getCustomValue();
        try {
            for (int i = 0; i < obj.length(); i++) {
                JSONObject tmp = obj.getJSONObject(i);
                list.add(new EditItem(tmp.getString("key"), tmp.getString("name"), tmp.getString("desc")));
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return list;
    }

    protected JSONArray getCustomValue() {
        String s = SPHelper.getInstance(getContext()).getString(getClass().getSimpleName() + "_word", "[]");
        JSONArray obj;
        try {
            obj = new JSONArray(s);
        } catch (Throwable e) {
            obj = new JSONArray();
        }
        return obj;
    }

    protected abstract RecyclerView getCustomRecyclerView();

    @Override
    public void update() {
        super.update();
        JSONArray array = getCustomValue();
        JSONArray result = new JSONArray();
        try {
            for (int i = 0; i < array.length(); i++) {
                JSONObject tmp = array.getJSONObject(i);
                if (new EditItem(tmp.getString("key")).show) {
                    result.put(tmp);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        TemplateAdapter adapter = new TemplateAdapter(result, this, getTemplateItemLayout());
        getCustomRecyclerView().setAdapter(adapter);
        h.post(() -> invalidate());
    }

    protected int getTemplateItemLayout() {
        return R.layout.template_item;
    }
}
